<?php
/**
 * 我要成为分销商
 */
namespace frontend\controllers;

use bc\controller\FrontController;
use bc\model\VendorCooperateformModel;
use bc\service\DictCityService;
use bc\service\DictDistrictService;
use bc\service\DictProvinceService;
use yii\web\Session;


class VendorController extends FrontController
{
	

	public function actionIndex()
	{
		$this->setOAuth();
		//获取省
		$provinceService = new DictProvinceService();
		$provinceList = $provinceService->getList();
		
		$view['province'] = $provinceList;
		return $this->render('index', $view);
	}
	
	public function actionSave()
	{
		$session = new Session();
		$user = $session->get('user');
		
		if(!$user)
			$this->redirect('/');
		//表单保存
		if($this->getRequest()->isPost == true)
		{
			$data['name'] = $this->post('name');
			$data['telephone'] = $this->post('telephone');
			$data['email'] = $this->post('email');
			$data['province_id'] = $this->post('province');
			$data['city_id'] = $this->post('city');
			$data['district_id'] = $this->post('district');
			$data['address'] = $this->post('address');
			$data['career'] = $this->post('career');
			$data['company'] = $this->post('company');
			$data['description'] = $this->post('description');
// 			$data['bank_deposit'] = $this->post('bank');
// 			$data['card'] = $this->post('cardnumber');
			$data['create_time'] = time();
				
			$data['open_id'] = $user['openid'];
			$data['nickname'] = $user['nickname'];
			$data['uid'] = $user['id'];	
			
			$provinceService = new DictProvinceService();
			$province  = $provinceService->getRow(['id' => $data['province_id']] );
	
			$cityService = new DictCityService();
			$city = $cityService->getRow(['id' => $data['city_id']]);
	
			$districtService = new DictDistrictService();
			$district = $districtService->getRow(['id' => $data['district_id']] );
	
			$data['province'] = $province['province_name'];
			$data['city'] = $city['city_name'];
			$data['district'] = $district['district_name'];
				
			$model = new VendorCooperateformModel();
			$model->add($data);
			echo '<script>保存成功</script>';
			echo '<script>window.location.href = "/organization"</script>';
		}
	
	}
	
	
	/**
	 * 根据省获取城市
	 */
	public function actionAjaxcitysbyprovincesid()
	{
		$provincesid = $this->post('value');
	
		$where = array('province_id' => $provincesid);
	
		$cityService = new DictCityService();
		$cityService->columns = 'id,city_name';
		$cityService->sort = 'id desc';
		$cityList = $cityService->getList($where);
		$result = array(
				'msg' => '获取成功',
				'result' => 1,
				'data' => $cityList
		);
		echo \json_encode($result);
	}
	
	/**
	 * 根据城市获取地区
	 */
	public function actionAjaxareasbycityid(){
		$cityid = $this->post('value');
		$where = array('city_id' => $cityid);
		$districtService = new DictDistrictService();
		$districtService->columns = 'id,district_name';
		$districtService->sort = 'id desc';
		$districtList = $districtService->getList($where);
		$result = array(
				'msg' => '获取成功',
				'result' => 1,
				'data' => $districtList
		);
		echo \json_encode($result);
	}
}

